<?php
/*
* @Author: 李明
* @Date: 2021-08-21 09:08:49
 * @LastEditTime: 2021-11-30 18:04:54
 * @LastEditors: 李明
* @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/task/TaskController.php
* @山东壹零贰肆信息科技有限公司
*/

namespace App\Http\Controllers\task;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\Role;
use GrahamCampbell\ResultType\Success;
use Illuminate\Http\Request;

class TaskController extends Controller
{
    /**
     * @description: 角色任务添加修改
     * @param {Request} $request
     * @return {*}
     */
    public function setRoleTask(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        if (isset($all['task'])) {
            $task = $all['task'];
        } else {
            return LMReturn::Error('缺少任务', 'TC001');
        }
        if (isset($all['role_id'])) {
            $role_id = $all['role_id'];
        } else {
            return LMReturn::Error('缺少角色', 'TC002');
        }
        if (isset($all['commission_rate'])) {
            $commission_rate = $all['commission_rate'];
        } else {
            return LMReturn::Error('缺少佣金', 'TC008');
        }
        try {
            $role = Role::where('id', $role_id)->first();
            $role->task = $task;
            $role->commission_rate = $commission_rate;
            $role->save();
        } catch (\Throwable $th) {
            return LMReturn::Error('设置角色任务失败', 'TC005');
        }
        return LMReturn::Success($role, '设置角色任务成功');
    }
    /**
     * @description: 部门任务添加修改
     * @param {Request} $request
     * @return {*}
     */
    public function setBranchTask(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        if (isset($all['task'])) {
            $task = $all['task'];
        } else {
            return LMReturn::Error('缺少任务', 'TC003');
        }
        if (isset($all['is_important'])) { //是否最重要，如果是，所有子部门跟随修改 1是 2否
            $is_important = $all['is_important'];
        } else {
            $is_important = 2;
        }
        if (isset($all['commission_rate'])) {
            $commission_rate = $all['commission_rate'];
        } else {
            return LMReturn::Error('缺少佣金', 'TC007');
        }
        if (isset($all['branch_id'])) {
            $branch_id = $all['branch_id'];
        } else {
            return LMReturn::Error('缺少部门', 'TC004');
        }
        try {
            $branch = Branch::where('id', $branch_id)->first();
            $branch->task = $task;
            $branch->is_important = $is_important;
            $branch->commission_rate = $commission_rate;
            $branch->save();
        } catch (\Throwable $th) {
            return LMReturn::Error('设置部门任务失败', 'TC006');
        }
        return LMReturn::Success($branch, '设置部门任务成功');
    }
}
